import { maintenanceHttp } from '/@/utils/http/axios';
import {
  CreateSubscriptionParams,
  CreateTopicParams,
  DeleteParams,
  DeleteTopicParams,
  DisplayNameParams,
  MessagePageParam,
  MessageSubsriptionResult,
  RequestSubscriptionParams,
  SelectTopicItem,
  TopicDetailItem,
  TopicDetailParams,
  TopicPageParams,
  TopicResult,
} from '/@/api/maintenance-center/message-notification/model/messageNotificationModel';

enum Api {
  GET_SUBSCRIPTION_LIST = '/v1/maintenance/subscription/getList',
  CREATE_SUBSCRIPTION = '/v1/maintenance/subscription/create',
  REQUEST_SUBSCRIPTION = '/v1/maintenance/subscription/requestSub',
  DELETE_SUBSCRIPTION = '/v1/maintenance/subscription/delete',
  CREATE_TOPIC = '/v1/maintenance/topic/create',
  DELETE_TOPIC = '/v1/maintenance/topic/delete',
  GET_TOPIC_DETAIL = '/v1/maintenance/topic/detail',
  GET_TOPIC_LIST = '/v1/maintenance/topic/topicList',
  CHANGE_DISPLAY_NAME = '/v1/maintenance/topic/update',
  SELECT_TOPIC_LIST = '/v1/maintenance/subscription/selectTopic',
}

export const getSubscriptionList = async (params: MessagePageParam) => {
  const result = await maintenanceHttp.get<MessageSubsriptionResult>({
    url: Api.GET_SUBSCRIPTION_LIST,
    params,
  });
  result.items.forEach((item) => {
    switch (item.status) {
      case 0:
        item.status = '未确认';
        item.buttonAllowed = false;
        break;
      case 1:
        item.status = '已确认';
        item.buttonAllowed = true;
        break;
      case 3:
        item.status = '已取消';
        item.buttonAllowed = true;
        break;
    }
  });
  return result;
};
export const createSubscription = (params: CreateSubscriptionParams) => {
  return maintenanceHttp.post<string>({ url: Api.CREATE_SUBSCRIPTION, params });
};
export const requestSubscription = (params: RequestSubscriptionParams) => {
  return maintenanceHttp.post<string>({
    url: `${Api.REQUEST_SUBSCRIPTION}/${params.subUrn}?endpoint=${params.endpoint}&protocol=${params.protocol}`,
  });
};
export const deleteSubscription = (params: DeleteParams) => {
  return maintenanceHttp.delete<string>({
    url: Api.DELETE_SUBSCRIPTION + '/' + params.subUrn,
  });
};
export const createOrUpdateTopic = (params: CreateTopicParams) => {
  return maintenanceHttp.post<string>({ url: Api.CREATE_TOPIC, params });
};
export const deleteTopic = (params: DeleteTopicParams) => {
  return maintenanceHttp.delete<string>({
    url: Api.DELETE_TOPIC + '/' + params.topicUrn,
  });
};
export const getTopicList = (params: TopicPageParams) => {
  return maintenanceHttp.get<TopicResult>({ url: Api.GET_TOPIC_LIST, params });
};
export const getTopicDetail = async (params: TopicDetailParams) => {
  const result: TopicDetailItem = await maintenanceHttp.get<TopicDetailItem>({
    url: Api.GET_TOPIC_DETAIL + '/' + params.topicUrn + '?current=' + params.current + '&size=' + params.size,
  });
  result.subscriptions.records.forEach((item) => {
    switch (item.status) {
      case 0:
        item.status = '未确认';
        item.buttonAllowed = false;
        break;
      case 1:
        item.status = '已确认';
        item.buttonAllowed = true;
        break;
      case 3:
        item.status = '已取消';
        item.buttonAllowed = true;
        break;
    }
  });
  return result;
};
export const changeDisplayName = (params: DisplayNameParams) => {
  return maintenanceHttp.put<string>({
    url: `${Api.CHANGE_DISPLAY_NAME}/${params.topicUrn}?newName=${params.newName}`,
  });
};

export const selectTopicList = () => {
  return maintenanceHttp.get<SelectTopicItem>({ url: Api.SELECT_TOPIC_LIST });
};
